package com.bocom.hairy.crab.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bocom.hairy.crab.base.entity.User;
import com.bocom.hairy.crab.base.mapper.UserMapper;
import com.bocom.hairy.crab.base.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author jiangshikun
* @description 针对表【TB_USER(用于测试的用户表)】的数据库操作Service实现
* @createDate 2025-09-03 10:15:47
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{
    @Override
    public IPage<User> getUserPage(int page, int size, User user) {
        Page<User> pageParam = new Page<>(page, size);
        return baseMapper.selectUserPage(pageParam, user);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(List<User> userList) {
        if (userList == null || userList.isEmpty()) {
            return false;
        }
        return baseMapper.insertBatch(userList) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateBatch(List<User> userList) {
        if (userList == null || userList.isEmpty()) {
            return false;
        }
        return baseMapper.updateBatch(userList) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeBatchByIds(List<String> userIds) {
        if (userIds == null || userIds.isEmpty()) {
            return false;
        }
        return baseMapper.deleteBatchByIds(userIds) > 0;
    }

    @Override
    public User getById(String userId) {
        return baseMapper.selectOne(
                new LambdaQueryWrapper<User>()
                        .eq(User::getUserId, userId)
        );
    }

    @Override
    public boolean removeById(String userId) {
        return baseMapper.deleteById(userId) > 0;
    }
}




